package com.gxan.service.system;

import com.gxan.common.JsonResult;
import com.gxan.common.R;
import com.gxan.dao.impl.system.SystemDaoImpl;
import com.gxan.dao.system.SystemDao;
import com.gxan.pojo.*;
import com.gxan.service.SystemService;


import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class SystemServiceImpl implements SystemService {
    private SystemDao systemDao=new SystemDaoImpl();
    @Override
    public R addDepartment(String dept_full_name, String dept_short_name, String dept_father, String dept_address, String common, String remark, String start)  {
        int i =0;
        R r= new R();
        try {
            System.out.println("service--------dept");
            i=systemDao.addDepartment(dept_full_name, dept_short_name, dept_father, dept_address, common, remark, Integer.parseInt(start));
            r.setCode(200);
            r.setMsg("数据插入成功");
            r.setData(i);
            return r;
        } catch (SQLException e) {
            r.setCode(500);
            r.setMsg("数据插入失败");
            r.setData(i);
            return r;
        }


    }

    @Override
    public JsonResult queryAllUser(Integer currentPage, Integer pageSize) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer integer = systemDao.selectUserCount();
            jsonResult.setCount(integer);
            Integer param01 = (currentPage - 1) * pageSize;
            Integer param02 = pageSize;
            List<SysUser> userVos = systemDao.selectUSerAll(param01, param02);
            jsonResult.setData(userVos);
            if (jsonResult!=null){
                //LayUI规定数据表格数据可以展示，code必须是0
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差了，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public R addUser(String login_name, Integer password, String role, String dept_name, Integer phone) {
        R r=new R();

        try {
            int sysDepts = systemDao.selectDepartByName(dept_name);
            int i = systemDao.addUser(login_name, password, role, sysDepts, phone);
            r.setData(i);
            if(i>0){
                r.setCode(200);
                r.setMsg("数据添加成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public R updateUser(Integer login_id,String login_name,  String role, String dept_name, String phone) {
        R r=new R();

        try {
            int sysDepts = systemDao.selectDepartByName(dept_name);
            int i = systemDao.updateUser(login_id,login_name, role, sysDepts, phone);
            r.setData(i);
            if(i>=0){
                r.setCode(200);
                r.setMsg("数据修改成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public R delUserById(Integer id) {
        R r=new R();
        try {
            if(id==24){
                r.setCode(500);
                r.setMsg("系统开小差了，请联系管理员");
                return r;
            }
            int i = systemDao.delUserById(id);
            r.setData(i);
            if(i>=0){
                r.setCode(200);
                r.setMsg("数据删除成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public JsonResult selectByNameRoleDepartment(String login_user, String role, String dept_name,Integer currentPage,Integer pageSize) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer param01 = (currentPage - 1) * pageSize;
            Integer param02 = pageSize;
            Integer integer = systemDao.selectCountByNameRoleDepartment(login_user,role,dept_name,param01, param02);
            jsonResult.setCount(integer);
            List<SysUser> userVos = systemDao.selectByNameRoleDepartment(login_user,role,dept_name,param01, param02);
            jsonResult.setData(userVos);
            if (jsonResult!=null){
                //LayUI规定数据表格数据可以展示，code必须是0
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差了，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public JsonResult selectAllDeptname() {
        JsonResult jsonResult = new JsonResult();
        try {
            List<SysDept> userVos = systemDao.selectAllDeptname();
            jsonResult.setData(userVos);
            jsonResult.setCount(userVos.size());
            if (jsonResult!=null){
                //LayUI规定数据表格数据可以展示，code必须是0
                jsonResult.setCode(200);
                jsonResult.setMsg("查询成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差了，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public R addPosition(String job_name, String job_type, String dept_name, String description, String remark, Integer status) {
        R r=new R();
        Integer dept_id = null;
        try {
            int sysDepts = systemDao.selectDepartByName(dept_name);
            int i = systemDao.addPosition(job_name, job_type,sysDepts, description, remark,status);
            r.setData(i);
            if(i>0){
                r.setCode(200);
                r.setMsg("数据添加成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public JsonResult selectAllPosition(int currentPage, int pageSize) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer integer = systemDao.selectPositionCount();
            jsonResult.setCount(integer);
            Integer param01 = (currentPage - 1) * pageSize;
            Integer param02 = pageSize;
            List<SysJob> userVos = systemDao.selectAllPosition(param01, param02);
            jsonResult.setData(userVos);
            if (jsonResult!=null){
                //LayUI规定数据表格数据可以展示，code必须是0
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差了，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public R deletePositionById(int id) {
        R r=new R();
        try {
            if(id==1){
                r.setCode(500);
                r.setMsg("系统开小差了，请联系管理员");
                return r;
            }
            int i = systemDao.deletePositionById(id);
            r.setData(i);
            if(i>=0){
                r.setCode(200);
                r.setMsg("数据删除成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public R updatePositionById(Integer job_id,String job_name, String job_type, String dept_name, String description, String remark, Integer status) {
        R r=new R();
        System.out.println("service----position");
        try {
            int sysDepts = 0;
            if(dept_name!=null){
                sysDepts = systemDao.selectDepartByName(dept_name);
            }
            System.out.println("通过部门名查部门id");
            System.out.println("sysDepts="+sysDepts);
            int i = systemDao.updatePositionById(job_id,job_name,job_type,sysDepts, description,remark,status);
            r.setData(i);
            System.out.println("数据修改成功");
            if(i>=0){
                r.setCode(200);
                r.setMsg("数据修改成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public JsonResult selectPositionByNameDepartmentStart(String job_name, String dept_name, int status, Integer page, Integer limit) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer param01 = (page - 1) * limit;
            Integer param02 = limit;
            //根据职位名称，职位部门和是否启用查职位表数据总条数
            Integer integer = systemDao.selectCountPositionByNameDepartmentStart(job_name,dept_name,status,param01, param02);
            jsonResult.setCount(integer);
            //根据职位名称，职位部门和是否启用查职位表数据
            List<SysJob> userVos = systemDao.selectPositionByNameDepartmentStart(job_name,dept_name,status,param01, param02);
            jsonResult.setData(userVos);
            if (jsonResult!=null){
                //LayUI规定数据表格数据可以展示，code必须是0
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差了，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public R addJobtitle(String job_title_name, String dept_name, String description, String remark, Integer status) {
        R r=new R();
        Integer dept_id = null;
        try {
            int sysDepts = systemDao.selectDepartByName(dept_name);
            int i = systemDao.addJobtitle(job_title_name, sysDepts, description, remark,status);
            r.setData(i);
            if(i>0){
                r.setCode(200);
                r.setMsg("数据添加成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public JsonResult selectJobtitleAll(int currentPage, int pageSize) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer integer = systemDao.selectJobtitleCount();
            jsonResult.setCount(integer);
            Integer param01 = (currentPage - 1) * pageSize;
            Integer param02 = pageSize;
            List<SysJobTitle> userVos = systemDao.selectJobtitleAll(param01, param02);
            jsonResult.setData(userVos);
            if (jsonResult!=null){
                //LayUI规定数据表格数据可以展示，code必须是0
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差了，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public R deleteJobtitleById(int id) {
        R r=new R();
        try {
            if(id==1){
                r.setCode(500);
                r.setMsg("系统开小差了，请联系管理员");
                return r;
            }
            int i = systemDao.deleteJobtitleById(id);
            r.setData(i);
            if(i>=0){
                r.setCode(200);
                r.setMsg("数据删除成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public R updateJobtitleById(Integer job_title_id, String job_title_name, String dept_name, String description, String remark, Integer status) {
        R r=new R();
        System.out.println("service----position");
        try {
            int sysDepts = 0;
            if(dept_name!=null){
                sysDepts = systemDao.selectDepartByName(dept_name);
            }
            System.out.println("通过部门名查部门id");
            System.out.println("sysDepts="+sysDepts);
            int i = systemDao.updateJobtitleById(job_title_id,job_title_name,sysDepts, description,remark,status);
            r.setData(i);
            System.out.println("数据修改成功");
            if(i>=0){
                r.setCode(200);
                r.setMsg("数据修改成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public JsonResult selectJobtitleByNameDepartmentStatus(String job_title_name, String dept_name, int status, Integer page, Integer limit) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer param01 = (page - 1) * limit;
            Integer param02 = limit;
            //根据职位名称，职位部门和是否启用查职位表数据总条数
            Integer integer = systemDao.selectCountJobtitleByNameDepartmentStatus(job_title_name,dept_name,status,param01, param02);
            jsonResult.setCount(integer);
            //根据职位名称，职位部门和是否启用查职位表数据
            List<SysJobTitle> userVos = systemDao.selectJobtitleByNameDepartmentStatus(job_title_name,dept_name,status,param01, param02);
            jsonResult.setData(userVos);
            if (jsonResult!=null){
                //LayUI规定数据表格数据可以展示，code必须是0
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差了，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public R addTest(String test_title, String test_type, String dept_name, String test_level, String test_setter, Date date, String test_content, String remark, String test_url,Integer status) {
        R r=new R();
        Integer dept_id = null;
        try {
            int sysDepts = systemDao.selectDepartByName(dept_name);
            int i = systemDao.addTest(test_title, test_type, sysDepts, test_level,test_setter,date,test_content,remark,test_url,status);
            r.setData(i);
            if(i>0){
                r.setCode(200);
                r.setMsg("数据添加成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public JsonResult selectTestAll(Integer currentPage, Integer pageSize) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer integer = systemDao.selectTestCount();
            jsonResult.setCount(integer);
            Integer param01 = (currentPage - 1) * pageSize;
            Integer param02 = pageSize;
            List<Test> userVos = systemDao.selectTestAll(param01, param02);
            jsonResult.setData(userVos);
            if (jsonResult!=null){
                //LayUI规定数据表格数据可以展示，code必须是0
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差了，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public R updateTestById(Integer test_id, String test_title, String test_type, String dept_name, String test_level, String test_setter, Date date, String test_content, String remark, String test_url, Integer status) {
        R r=new R();
        System.out.println("service----position");
        try {
            int sysDepts = 0;
            if(dept_name!=null){
                sysDepts = systemDao.selectDepartByName(dept_name);
            }
            System.out.println("通过部门名查部门id");
            System.out.println("sysDepts="+sysDepts);
            int i = systemDao.updateTestById(test_id,test_title, test_type, sysDepts, test_level,test_setter,date,test_content,remark,test_url,status);
            r.setData(i);
            System.out.println("数据修改成功");
            if(i>=0){
                r.setCode(200);
                r.setMsg("数据修改成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public R deleteTestById(int id) {
        R r=new R();
        try {
            if(id==1){
                r.setCode(500);
                r.setMsg("系统开小差了，请联系管理员");
                return r;
            }
            int i = systemDao.deleteTestById(id);
            r.setData(i);
            if(i>=0){
                r.setCode(200);
                r.setMsg("数据删除成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public JsonResult selectTestByTitleTypeDepartmentStart(String test_title, String test_type, String dept_name, int status, Integer page, Integer limit) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer param01 = (page - 1) * limit;
            Integer param02 = limit;
            //根据职位名称，职位部门和是否启用查职位表数据总条数
            Integer integer = systemDao.selectTestCountByTitleTypeDepartmentStart(test_title,test_type,dept_name,status,param01, param02);
            jsonResult.setCount(integer);
            //根据职位名称，职位部门和是否启用查职位表数据
            List<Test> userVos = systemDao.selectTestByTitleTypeDepartmentStart(test_title,test_type,dept_name,status,param01, param02);
            jsonResult.setData(userVos);
            if (jsonResult!=null){
                //LayUI规定数据表格数据可以展示，code必须是0
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差了，请联系管理员");
        }
        return jsonResult;
    }


}
